﻿
CREATE PROCEDURE [dbo].[resuma_CalculSporTrigger] 
	-- Add the parameters for the stored procedure here
	@IdPersoana int,
	@IdAngajatSalarizare int, 
	@IdTipSpor int,
	@Procent decimal(6,2) out,
	@Valoare decimal(18,2) out
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
    SET @Procent = 0
    SET @Valoare = 0
    DECLARE @BazaCalculSpor varchar(2)
    DECLARE @Format varchar(1)
    DECLARE @IDOcupatie int
    DECLARE @ProcentG decimal(6,2)
    
    -- Insert statements for procedure here
	SELECT @Procent = Valoare,@BazaCalculSpor = BazaCalculSpor,@Format=Format FROM [TipSpor] WHERE Activ=1 AND IDTipSpor = @IdTipSpor
	IF @Format is null
		return
	
	--Daca e de tip grila ocupatie
	SELECT TOP 1 @ProcentG=Valoare FROM AngajatiOcupatii angO JOIN
	GrileOcupatii grO ON angO.IDOcupatie=grO.IDOcupatie
	WHERE IDPersoana = @IdPersoana AND @Format='F' 
	ORDER BY DeLaData DESC 
	
	IF @Format='F' AND @ProcentG is null
	BEGIN
		SET @Procent = 0
		return
	END
	ELSE
		set @Procent = @ProcentG
	
	SELECT @Valoare = Salar*@Procent/100 FROM [AngajatiSalarizare] as angS 
	  WHERE IDAngajatSalarizare = @IdAngajatSalarizare and @BazaCalculSpor = 'SI'
END